/**
 * strictc - the Strict compiler
 * Copyright (C) 2008 David Olsson (vonolsson@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "ClassNode.h"
#include "FunctionNode.h"
#include "InterfaceNode.h"
#include "NamespaceNode.h"
#include "ProgramNode.h"
#include "SourceFileNode.h"
#include "Visitor.h"

using namespace std;

SourceFileNode::SourceFileNode(const string &name) throw(bad_alloc): NamedScopeNode(name), m_rootType(FRT_CLASS), m_fileRoot(0) {
}

SourceFileNode::~SourceFileNode() {
    for(list<FunctionNode *>::iterator iter = m_functions.begin(); iter != m_functions.end(); iter++) {
        delete(*iter);
    }
    if(m_fileRoot != 0) {
        delete(m_fileRoot);
    }
}

void SourceFileNode::setClass(ClassNode *cls) throw() {
    setFileRoot(cls, FRT_CLASS);
}

void SourceFileNode::setInterface(InterfaceNode *iface) throw() {
    setFileRoot(iface, FRT_INTERFACE);
}

void SourceFileNode::setNamespace(NamespaceNode *ns) throw() {
    setFileRoot(ns, FRT_NAMESPACE);
}

void SourceFileNode::addFunction(FunctionNode *func) throw(std::bad_alloc) {
    if(m_functions.empty() && m_fileRoot != 0) {
        m_fileRoot->setNextSibling(func);
    }
    
    m_functions.push_back(func);
    
    func->setParent(this);
}

const Node *SourceFileNode::firstChild() const throw() {
    if(m_fileRoot != 0) {
        return(m_fileRoot);
    }
    else if(!m_functions.empty()) {
        return(m_functions.front());
    }
    else {
        return(0);
    }
}

Node *SourceFileNode::firstChild() throw() {
    if(m_fileRoot != 0) {
        return(m_fileRoot);
    }
    else if(!m_functions.empty()) {
        return(m_functions.front());
    }
    else {
        return(0);
    }
}

ClassNode *SourceFileNode::classByName(const string &name) throw() {
    return(0);
}

FunctionNode *SourceFileNode::functionByName(const string &name) throw() {
    return(0);
}

InterfaceNode *SourceFileNode::interfaceByName(const string &name) throw() {
    return(0);
}

NamespaceNode *SourceFileNode::namespaceByName(const string &name) throw() {
    return(0);
}

void SourceFileNode::preAccept(Visitor &visitor) throw() {
    visitor.preVisit(*this);
}

void SourceFileNode::postAccept(Visitor &visitor) throw() {
    visitor.postVisit(*this);
}

void SourceFileNode::setFileRoot(Node *fileRoot, FileRootType rootType) throw() {
    if(m_fileRoot != 0) {
        delete(m_fileRoot);
    }
    
    m_fileRoot = fileRoot;
    m_rootType = rootType;
    
    if(!m_functions.empty()) {
        m_fileRoot->setNextSibling(m_functions.front());
    }
    
    m_fileRoot->setParent(this);
}
